package com.hna.eking.AirlineServer.Platform.Airport;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hna.eking.AirlineServer.Exception.RunServiceException;
import com.hna.eking.AirlineServer.Mapper.AirportInfo;
import com.hna.eking.AirlineServer.Service.AirportInfoService;
import com.hna.eking.AirlineServer.Utils.ResponseSupport;



/**
 *
 *  关于机场控制的controller
 * <p>Date : 2018-9-7</p>
 * <p>Description: 机场信息查询</p>
 * <p>Copyright: Copyright &copy; 2018</p>
 * <p>Company :aivac</p>
 * 
 * @author APP BD lele
 * @version  V1.0
 * <p>------------------------------------------------------------</p>
 * <p> 修改历史</p>
 * <p> 序号 日期 修改人 修改原因</p>
 * <p> 1 </p>
 */

@Controller
@RequestMapping(value = "/airport")
public class AirportController {
	
	Logger logger = LoggerFactory.getLogger(AirportController.class);
	@Autowired
	AirportInfoService airportInfoService;
	
	/*
	 * 查询指定页航图信息 
	 * 	参数： 机场代码：airportcode 
	 * 	省：province 模糊匹配 
	 * 	机场名：airportname 模糊匹配
	 * 	返回条数：limit 
	 * 	跳过条数：offset 
	 * 返回： 航图信息列表 {count：查询总条数，maplist:[航图列表]}
	 *
	 */
	@RequestMapping(value = "/airportlist", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody  ResponseSupport<Map<String, Object>> AirportInfoListByInput(
			@RequestParam(value = "airportcode", required = false) String airportcode,
			@RequestParam(value = "province", required = false) String province,
			@RequestParam(value = "airportname", required = false) String airportname,
			@RequestParam(value = "page", required = true) Integer page,
			@RequestParam(value = "num", required = true) Integer num) {

		String ret = null;
		ResponseSupport<Map<String, Object>> rsp = new ResponseSupport<Map<String, Object>>();
		try {
			Map<String, Object>  maplist = airportInfoService.SelectListByInput(airportcode, province, airportname, num, (page - 1) * num);
			
			rsp.setCode(RunServiceException.SUCCESS);
			rsp.setContent(maplist);
		} catch (RunServiceException ex) {
			ex.printStackTrace();		
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
		}
		return rsp;
	}
	
	/*
	 * 插入指机场信息信息 
	 * 参数： 航图信息：AirportInfo
	 * 
	 * 返回： 200-成功
	 *
	 */
	@RequestMapping(value = "/airportinfoinsert", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<String> InsertAirportInfo(
			@RequestParam(value = "airportinfo", required = true) String AirportInfoParm) {
		ResponseSupport<String> rsp = new ResponseSupport<String>();
		ObjectMapper mapper = new ObjectMapper();
		AirportInfo airportinfo = new AirportInfo();

		try {
			JsonNode rootNode = mapper.readTree(AirportInfoParm);
			String airportCode = rootNode.path("airportcode").textValue();
			String User = rootNode.path("createuser").textValue();
			/* 解析接口 */
			airportinfo.setAirportCode(airportCode);
			airportinfo.setAirportName(rootNode.path("airportname").textValue());
			airportinfo.setShortName(rootNode.path("shortname").textValue());
			airportinfo.setEnglishName(rootNode.path("englishname").textValue());
			airportinfo.setAtis(rootNode.path("atis").textValue());
			airportinfo.setCountry(rootNode.path("country").textValue());
			airportinfo.setProvince(rootNode.path("province").textValue());
			airportinfo.setCity(rootNode.path("city").textValue());
			airportinfo.setCreateUser(User);
			airportinfo.setShortNameEn(rootNode.path("shortnameen").textValue());

			/* 插入机场信息 */
			airportInfoService.AirportInsert(airportinfo);
			rsp.setCode(RunServiceException.SUCCESS);
			return rsp;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(RunServiceException.JSON_FORMAT_ERROR);
			rsp.setMessage(RunServiceException.getMessage(RunServiceException.JSON_FORMAT_ERROR));
			return rsp;
		} catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
			return rsp;
		}
	}
	
	/*
	 * 更新指机场信息信息 
	 * 参数： 航图信息：AirportInfo
	 * 
	 * 返回： 200-成功 
	 *
	 */
	@RequestMapping(value = "/airportinfoupdate", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<String> UpdatetAirportInfo(
			@RequestParam(value = "airportinfo", required = true) String AirportInfoParm) {
		ResponseSupport<String> rsp = new ResponseSupport<String>();
		ObjectMapper mapper = new ObjectMapper();
		AirportInfo airportinfo = new AirportInfo();

		try {
			JsonNode rootNode = mapper.readTree(AirportInfoParm);
			String airportCode = rootNode.path("airportcode").textValue();
			String User = rootNode.path("updateuser").textValue();
			/* 解析接口 */
			airportinfo.setAirportCode(airportCode);
			airportinfo.setAirportName(rootNode.path("airportname").textValue());
			airportinfo.setShortName(rootNode.path("shortname").textValue());
			airportinfo.setEnglishName(rootNode.path("englishname").textValue());
			airportinfo.setAtis(rootNode.path("atis").textValue());
			airportinfo.setCountry(rootNode.path("country").textValue());
			airportinfo.setProvince(rootNode.path("province").textValue());
			airportinfo.setCity(rootNode.path("city").textValue());
			airportinfo.setUpdateUser(User);
			airportinfo.setShortNameEn(rootNode.path("shortnameen").textValue());

			/* 更新机场 */
			airportInfoService.AirportUpdate(airportinfo);
			rsp.setCode(RunServiceException.SUCCESS);
			return rsp;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rsp.setCode(RunServiceException.JSON_FORMAT_ERROR);
			rsp.setMessage(RunServiceException.getMessage(RunServiceException.JSON_FORMAT_ERROR));
			return rsp;
		} catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
			return rsp;
		}
	}
	
	/*
	 * 逻辑删除指定机场 
	 * 参数： 
	 * 机场代码：airportcode 
	 * 
	 * 返回： 200-成功 
	 *
	 */
	@RequestMapping(value = "/airportinfodel", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<String> DeleteAirportInfo(
			@RequestParam(value = "airportcode", required = true) String airportcode,
			@RequestParam(value = "user", required = true) String user) {
		ResponseSupport<String> rsp = new ResponseSupport<String>();

		try {
			/* 删除机场航图版本 */
			airportInfoService.DeleteAirportInfo(airportcode, user);
			rsp.setCode(RunServiceException.SUCCESS);
			return rsp;
		} catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
			return rsp;
		}
	}
	/*
	 * 查询所有有效机场机场代码和名称 
	 * 参数： 
	 * 
	 * 
	 * 返回：{"airportcode":"","airportname":""}
	 * 	 *
	 */
	@RequestMapping(value = "/airportalleft", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody ResponseSupport<List<Map>> SelectAllEft() {
		ResponseSupport<List<Map>> rsp = new ResponseSupport<List<Map>>();

		try {
			
			List<Map> list = airportInfoService.SelectAllEft();
			rsp.setCode(RunServiceException.SUCCESS);
			rsp.setContent(list);
		} catch (RunServiceException ex) {
			ex.printStackTrace();
			rsp.setCode(ex.getCode());
			rsp.setMessage(ex.getMessage());
			
		}
		return rsp;
	}
}
